package org.apache.struts.ehr.patient_details.action;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.ehr.patient_details.model.HL7Message;
import org.apache.struts.ehr.patient_details.model.PatientDetails;
import org.apache.struts.ehr.patient_details.model.SelectLists;
import org.apache.struts2.interceptor.ServletRequestAware;

import ca.uhn.hl7v2.HL7Exception;
import ca.uhn.hl7v2.model.Message;
import ca.uhn.hl7v2.model.v251.message.ADR_A19;
import ca.uhn.hl7v2.parser.EncodingNotSupportedException;
import ca.uhn.hl7v2.parser.PipeParser;

import com.earogya.Constants;
import com.earogya.communicate.Communicator;
import com.earogya.logic.patient_details.ADR_A19_Handler;
import com.earogya.logic.patient_details.CreateQRYA19;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 
 * Action for More search results
 * 
 * @author Ureka
 *
 */
public class MoreSearchAction extends ActionSupport implements ServletRequestAware{

	private PatientDetails patientDetails;
	private HL7Message message;
	private Date date;
	private HttpServletRequest request;
	private List <PatientDetails> patientDetailsList;
	private SelectLists selectLists;
	private String email;
	private String telephoneNo;
	
	
	/**
	 * 
	 * Override the execute method for more search action
	 * 
	 */

	public String execute() throws Exception {
		Map<String, Object> session=ActionContext.getContext().getSession();
		patientDetails.getPatient().setPatientId(request.getParameter("patient_id"));
		CreateQRYA19 createMessage=new CreateQRYA19(patientDetails);
		String msg=createMessage.createMessage(Constants.SENDING_FACILITY);
		message=new HL7Message();
		if(!msg.equals(null)){
			Communicator c=new Communicator();
			String reply=c.sendMessage(msg);
			message.setMessage(msg+"-------------"+reply);
			System.out.print(reply);
			ADR_A19_Handler handler=new ADR_A19_Handler(createADRA19Msg(reply.toString()));
			patientDetailsList=handler.manageMessage();
		this.patientDetails=patientDetailsList.get(0);
		if(patientDetails.getPatientEmailList().size()>0){
		email=patientDetails.getPatientEmailList().get(0).getEmail();
		}
		if(patientDetails.getPatientTelephoneList().size()>0){
		telephoneNo=patientDetails.getPatientTelephoneList().get(0).getTelephoneNo();
		}
		this.selectLists=new SelectLists();
			
		}
				else{
			message.setMessage(Constants.EMPTY);
		}
		
		return SUCCESS;

	}

	public ADR_A19 createADRA19Msg(String message) throws EncodingNotSupportedException, HL7Exception{
		Message hapiMsg  = new PipeParser().parse(message);
		return (ADR_A19)hapiMsg;
	}
	
	public HL7Message getMessage() {
		return message;
	}

	public MoreSearchAction(){
		patientDetails=new PatientDetails();
	}
	public void setServletRequest(HttpServletRequest arg0) {
		this.request = arg0;
	}
	public PatientDetails getPatientDetails() {
		return patientDetails;
	}

	public void setPatientDetails(PatientDetails patientDetails) {
		this.patientDetails = patientDetails;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getTelephoneNo() {
		return telephoneNo;
	}

	public void setTelephoneNo(String telephoneNo) {
		this.telephoneNo = telephoneNo;
	}

	public SelectLists getSelectLists() {
		return selectLists;
	}

	public void setSelectLists(SelectLists selectLists) {
		this.selectLists = selectLists;
	}
}
